<?php

class CategorybrandsController extends AppController {

    var $name = 'Categorybrands';
    var $uses = array('Categorybrand', 'Brand');
    var $helpers = array('Html', 'Ajax', 'Javascript');
    var $components = array('RequestHandler');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allowedActions = array('*');
    }

    function autoComplete() {
        $this->set('brands', $this->Brand->find('all', array(
                    'conditions' => array(
                        'Brand.brand_description LIKE' => $this->data['Brand']['brand_description'] . '%'
                    ),
                    'fields' => array('brand_description')
                )));
        $this->layout = 'ajax';
    }

    function sugestion($id=null) {
        $this->layout = false;
        $brand = $this->Brand->find('list', array('conditions' => array('Brand.brand_description LIKE' => '%' . $id . '%')));
        $this->autoRender = false;
    }

    function index($id = null, $id2=null) {
        $this->layout = false;
        //$this->Categorybrand->recursive=0;
        $fields = array('Category.category_name', 'Categorybrand.id', 'Categorybrand.brand_id', 'Categorybrand.category_id', 'Brand.brand_description');
        $conditions = array('Categorybrand.category_id' => $id2, 'Categorybrand.brand_type' => $id);
        $order = array('Categorybrand.id' => 'DESC');
        $this->data['Categorybrands'] = $this->Categorybrand->find('all', array(
            'fields' => $fields,
            'conditions' => $conditions,
            'order' => $order
                ));
        //pr($this->data['Categorybrands']);
        $this->data['brand_type'] = $id;
        $this->data['cat_id'] = $id2;
    }

    function view($id = null) {
        $this->layout = false;
        if (!$id) {
            $this->Session->setFlash(__('Invalid categorybrand', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('categorybrand', $this->Categorybrand->read(null, $id));
    }

    function add($id=null) {
        $this->layout = false;
        $cat_id = $this->data['Categorybrand']['category_id'];
        $brand_type = $this->data['Brand']['brand_type'];

        if (!empty($this->data)) {
            $duplicate = $this->Brand->find('count', array('conditions' => array('Brand.brand_description' => $this->data['Brand']['brand_description'], 'Brand.brand_type' => $brand_type)));

            if ($duplicate > 0) {
                $this->Session->setFlash(__($this->data['Brand']['brand_description'] . " Duplicate Global " . $brand_type . ". Please use different.", true));
                $this->redirect(array('action' => 'index', $brand_type, $cat_id));
            } else {
                $this->Brand->create();
                //pr($this->data['Brand']);

                if ($this->Brand->save($this->data['Brand'])) {
                    $brand_id = $this->Brand->getlastinsertid();
                    $this->data['Categorybrand']['brand_id'] = $brand_id;
                    //$this->data['Categorybrand']['category_id']=$cat_id;
                    $this->data['Categorybrand']['brand_type'] = $brand_type;
                    $this->Categorybrand->create();
                    if ($this->Categorybrand->save($this->data['Categorybrand'])) {
                        $this->Session->setFlash(__('The category brand has been saved successfully.', true));
                        $this->redirect(array('action' => 'index', $brand_type, $cat_id));
                    } else {
                        $this->Session->setFlash(__('The category brand could not be saved. Please, try again.', true));
                        $this->redirect(array('action' => 'index', $brand_type, $cat_id));
                    }
                } else {
                    $this->Session->setFlash(__('The category brand could not be saved. Please, try again.', true));
                    $this->redirect(array('action' => 'index', $brand_type, $cat_id));
                }
            }
        } else {
            $this->Session->setFlash(__("Duplicate Global " . $brand_type . " try another.", true));
            $this->redirect(array('action' => 'index', $brand_type, $cat_id));
        }
        $this->redirect(array('action' => 'index', $brand_type, $cat_id));
        //$this->autoRender=false;
    }

    function edit() {
        $this->layout = false;
        $return_arr['boxID'] = $this->data['id'];
        $return_arr['brandName'] = $this->data['brand_description'];
        $duplicate = $this->Brand->find('count', array('conditions' => array('Brand.id !=' => $this->data['id'], 'Brand.brand_description' => $this->data['brand_description'], 'Brand.brand_type' => $this->data['brand_type'])));
        if ($duplicate > 0) {
            $return_arr['message'] = "Duplicate";
        } else {
            if ($this->Brand->save($this->data)) {
                $return_arr['message'] = "Yes";
            } else {
                $return_arr['message'] = "No";
            }
        }
        echo json_encode($return_arr);

        $this->autoRender = false;
    }

    function deletebrand($id = null, $id2=null) {
        $this->layout = false;
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for categorybrand', true));
            $this->redirect(array('action' => 'index', 'brand', $id2));
        }

        $b_id = $this->Categorybrand->findById($id, array('brand_id'));

        if ($this->Categorybrand->query("delete from authentic_categorybrands where id='" . $id . "'")) {
            //$this->Brand->query("delete from authentic_brands where id='".$b_id['Categorybrand']['brand_id']."'");
            $this->Session->setFlash(__('Categorybrand has been deleted succeessfully.', true));
            $this->redirect('/categorybrands/index/brand/' . $id2);
        }

        $this->Session->setFlash(__('Categorybrand was not deleted', true));
        $this->redirect(array('action' => 'index', 'brand', $id2));
    }

    function deletestyle($id = null, $id2=null) {
        $this->layout = false;
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for categorybrand', true));
            $this->redirect(array('action' => 'index', 'style', $id2));
        }
        $b_id = $this->Categorybrand->findById($id, array('brand_id'));

        if ($this->Categorybrand->query("delete from authentic_categorybrands where id='" . $id . "'")) {
            //$this->Brand->query("delete from authentic_brands where id='" . $b_id['Categorybrand']['brand_id'] . "'");
            $this->Session->setFlash(__('Categorybrand has been deleted succeessfully.', true));
            $this->redirect('/categorybrands/index/style/' . $id2);
        }
        $this->Session->setFlash(__('Categorybrand was not deleted', true));
        $this->redirect(array('action' => 'index', 'style', $id2));
    }

    function deletecondition($id = null, $id2=null) {
        $this->layout = false;
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for categorybrand', true));
            $this->redirect(array('action' => 'index', 'condition', $id2));
        }
        $b_id = $this->Categorybrand->findById($id, array('brand_id'));
        if ($this->Categorybrand->query("delete from authentic_categorybrands where id='" . $id . "'")) {
            // $this->Brand->query("delete from authentic_brands where id='" . $b_id['Categorybrand']['brand_id'] . "'");
            $this->Session->setFlash(__('Categorybrand has been deleted succeessfully.', true));
            $this->redirect('/categorybrands/index/condition/' . $id2);
        }
        $this->Session->setFlash(__('Categorybrand was not deleted', true));
        $this->redirect(array('action' => 'index', 'condition', $id2));
    }

    function backstage_index() {
        $this->Categorybrand->recursive = 0;
        $this->set('categorybrands', $this->paginate());
    }

    function backstage_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid categorybrand', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('categorybrand', $this->Categorybrand->read(null, $id));
    }

    function backstage_add() {
        if (!empty($this->data)) {
            $this->Categorybrand->create();
            if ($this->Categorybrand->save($this->data)) {
                $this->Session->setFlash(__('The categorybrand has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The categorybrand could not be saved. Please, try again.', true));
            }
        }
        $categories = $this->Categorybrand->Category->find('list');
        $brands = $this->Categorybrand->Brand->find('list');
        $this->set(compact('categories', 'brands'));
    }

    function backstage_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid categorybrand', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Categorybrand->save($this->data)) {
                $this->Session->setFlash(__('The categorybrand has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The categorybrand could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Categorybrand->read(null, $id);
        }
        $categories = $this->Categorybrand->Category->find('list');
        $brands = $this->Categorybrand->Brand->find('list');
        $this->set(compact('categories', 'brands'));
    }

    function backstage_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for categorybrand', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Categorybrand->delete($id)) {
            $this->Session->setFlash(__('Categorybrand deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Categorybrand was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

}

?>